<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://www.yaodian100.com/jsp/yaodian100" prefix="yaodian100" %>
<%@ include file="/taglibs.jsp"%>
<script type="text/javascript" charset="UTF-8" src="/js/formValidator.js" charset="UTF-8"></script>
<script type="text/javascript" charset="UTF-8" src="/js/formValidatorRegex.js" charset="UTF-8"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.alphanumeric.js"></script>	
<s:actionerror cssClass="errmsg"/>
<s:actionmessage cssClass="errmsg"/>
<br/>
  <h1>编辑资料 - 批次修改商品主分类</h1>
  <div class="batchstep">
    <div class="batchstep-2">
      <span class="step1">Step 1：查寻商品/选取商品</span>
      <span class="onstep2">Step 2：编辑资料</span>
      <span class="step3">Step 3：核对资料/完成编辑</span>
    </div>
  </div>
<script language="javascript">
$().ready( function() {
	$("#tmpShowSeq").numeric();
	$('input[id^=showSeq_]').numeric();	
	$("#mainCatSearchbtn").click(function(){
		//
		searchCategory($("#paramMainCategory").val(),"","");
	});

	/*$("input[name=pid]").each(function(index){
		var key = $(this).val();
		$("#mainCatSearchbtn_"+key).click(function(){
			
			searchCategory($("#categoryId_"+key).val(),"_"+key,"");
		});
	});*/

	$('input[id^=mainCatSearchbtn_]').click(function(){
		var key = $(this).attr('id').split("_")[1];
		//alert($("#categoryId_"+key).val());
		searchCategory($("#categoryId_"+key).val(),"_"+key,"");
	});

	
	$("#tmpShowSeq_Y").click(function() {
		$("#showSeq").hide();
	});

	
	$("#tmpShowSeq_N").click(function() {
		$("#showSeq").show();
	});
	
	$("#oldsubmit").click(function() {
		$("#dataoldornew").val("old");
		$("#newsubmit").attr("disabled",true);
		$("#paramMainCategory").attr("disabled",true); 
		$("#mainCatSearchbtn").attr("disabled",true); 
		$("#tmpShowSeq_Y").attr("disabled",true);
		$("#tmpShowSeq_N").attr("disabled",true);     
		$("#nextsubmit").hide();
		$("#errtable").hide();	
		$("#download").submit();
		
	});

	$("#mainSubmit").click(function(){
		if ( "" == $("#MultiFile8_F1").val()){
			alert("尚未选择上传档案，请重新选择！");
			return false;
		}
	});
	
});
function applyAll() {
	var  r  = "^[0-9]*[0-9][0-9]*$";
	var error = 0;
	if($.trim($("#paramMainCategory").val())==""){
		error++;
		$("#mainCatError").html("请输入分类编号");
		$("#categoryPath").addClass("errmsg");			
		$("#categoryPath").show();		
	}else{
		$("#categoryPath").hide();
	}

	if($(":radio[name='tmpShowSeqR']:checked").val()=="N" && $.trim($("#tmpShowSeq").val())=="" ||$(":radio[name='tmpShowSeqR']:checked").val()=="N" && !$("#tmpShowSeq").val().match(r)){
		error++;	
		$("#showSeqError").show();	
	}else{
		$("#showSeqError").hide();
	}	
	
	if(error==0){
		 searchCategory($("#paramMainCategory").val(),"","setall");
	}else{
		return;
	}
}
function setAll(){
	var err=0;
	$(':input[id^="categoryId_"]').attr("value", $("#paramMainCategory").val());
	
	if($(":radio[name='tmpShowSeqR']:checked").val()=="Y") {
		$("input[name=pid]").each(function(index){
			var key = $(this).val();
			$("#showSeq_"+key).val($("#oldShowSeq_"+key).val());
			
		});
	} else {
		$(':input[id^="showSeq_"]').attr("value", $("#tmpShowSeq").val());
	}
	sumbitF();
}
var error = 0;
var done=0;

function searchCategory(catId,key,func){
	/*if ( "failure" != categoryResult &&  "isEndnode" != categoryResult ){
		$("#categoryPath").html(categoryResult);		
	}else if ( "failure" == categoryResult ){
		$("#categoryPath").html("该分类未上架或不存在、已被下架、删除或无该权限查看");
	}else if ( "isEndnode" == categoryResult ){
		$("#categoryPath").html("请输入最底层的分类");
	}*/
	$("#categoryPath"+key).show();
	if($.trim(catId)==""){
		$("#mainCatError"+key).html("请输入分类编号");
		$("#categoryPath"+key).addClass("errmsg");
		error++;
	}else{
	$.post("<%=ServerValue.getMidContextPath()%>/productCategory/jsonCategory.do",{categoryId:catId},function(msg){
		if ( "failure" != msg &&  "isEndnode" != msg ){
			$("#mainCatError"+key).html(msg);
			$("#mainCategoryPathDesc"+key).val(msg);		
			$("#categoryPath"+key).removeClass("errmsg");
			if(func=="setall"){	
				if(error==0){				
					$("#dataoldornew").val("new");
					$("#oldsubmit").attr("disabled",true); 
					$("#nextsubmit").hide();
					$("#errtable").hide();	
					$("#download").submit();
				}				
			}					
		 	
		}else if ( "failure" == msg ){
			$("#mainCatError"+key).html("该分类未上架或不存在、已被下架、删除或无该权限查看");
			$("#categoryPath"+key).addClass("errmsg");
			error++;
		}else if ( "isEndnode" == msg ){
			$("#mainCatError"+key).html("请输入最底层的分类");
			$("#categoryPath"+key).addClass("errmsg");
			error++;
		}
		},"json");
	}		
}
function sumbitF(){
	error=0;
	done = 0;
	var last =$("input[name=pid]").length; 
	$("input[name=pid]").each(function(index){
			
			var key = $(this).val();
			
			//if($.trim($("#showSeq_"+key).val())==""){
				//error++;
				//$("#showSeqError_"+key).show();
			//}else{
				//$("#showSeqError_"+key).hide();
			//}
			searchCategory($("#categoryId_"+key).val(),"_"+key,"submit");
			/*if(error==0&&last==index+1){
				//alert("ready");
				searchCategory($("#categoryId_"+key).val(),"_"+key,"submit");
			}else{
				//alert("wait");
				searchCategory($("#categoryId_"+key).val(),"_"+key,"");
			}*/								
	});
	if(error>0){
		return;
	}
}
</script>
  <!-- div class="data-form"> -->
<s:form action="downloadFile.do" id="download" method="post">
	<s:hidden name="paramBatchEventId"/>
	<s:iterator value="detailMap" var="map" status="rows_index"></s:iterator>
	 <img src="/images/ecadmin/bianjiziliaoliucheng.jpg"></img>
	<div class="tipmsg" style="width:53%"><ul><li>系统会将你所选定的商品自动生成Excel报表，请在信息编辑完成后，下载报表。<br/>此阶段具有时效性，请在<Strong>25分钟</Strong>内完成编辑，并上传相关Excel报表</li></ul></div>
     <h2>下载报表</h2>	
		<table class="tb-edit" style="width:53%;clear:none;">
          <tr>
          	<th width="30%">分类编号</th>
          	<td>
          	<span><s:textfield id="paramMainCategory" name="paramMainCategory" size="6" maxlength="6"/></span><input type="button" id="mainCatSearchbtn" value="分类查寻" />	<br>              
          	 <ul id="categoryPath" class="errmsg" style="display:none"><li><span id="mainCatError"></span></li></ul>
          	 <input type="hidden"  id="mainCategoryPathDesc" value="">
          	</td>
          </tr>
          <tr>
          	<th>设定排序</th>
          	<td>
          	<input type="radio" checked="checked" value="Y" name="tmpShowSeqR" id="tmpShowSeq_Y" >保持原值
          	<input type="radio" value="N" name="tmpShowSeqR" id="tmpShowSeq_N" >手动设定<span style="display:none" id="showSeq"><input type="text" name="tmpShowSeq" id="tmpShowSeq" size="5" maxlength="5" value="0"></span>
          	<div class="errmsg" id="showSeqError" style="display:none"><ul><li><span>请输入排序数字，且应为正整数</span></li></ul></div>
          	</td>
          </tr>
          <tr>
          	<th></th>
          	<td>
          	<input type="button" name="submit1" id="newsubmit" value="全部套用，下载Excel报表 " onclick="applyAll()">
          	<input type="hidden" id="dataoldornew" name="dataoldornew">
            <c:if test="${!empty event.modifyDate}"><input type="button" id="oldsubmit" value="下载之前上传的报表 "></c:if>
          	</td>
          </tr>
        </table>
      <br />
	  </s:form>
      
      <br />
      <h2>上传报表</h2>
      	 <div class="tipmsg"  style="width:53%"><ul><li>请将核对编辑后的报表上传。</li></ul></div>         
		<s:form action="uploadFile.do" method="post" enctype ="multipart/form-data">
	    	<table style="width:53%;clear:none;" border="0" align="left" class="tb-data">		
		        <tr>
		          	<td><input id="MultiFile8_F1" class="multi accept-xls" maxlength="1" type="file" name="upload"  accept="xls"/>
		          	<s:submit id="mainSubmit" value="上传Excel报表" /><br><br>
		         	<c:if test="${!empty step2Upload}">
			         	<div style="width:100%;font-size: 15px;" id="errtable" >
			         	上传成功${uploadSuccess}笔<br />
			         	上传失败${uploadErr}笔<br />
				         	<c:if test="${!empty errId}">
				         	上传失败的商品编号（最多显示20笔）:
					         	<c:forEach items="${errId}" var="errId" varStatus="rows_index">
					         	${errId} / 
					         	</c:forEach>	         		
				         	</c:if>
			         	</div>
		         	</c:if>
		         	</td>
		       </tr>
	    	 </table>
	   </s:form>
       <s:form action="confirm.do">
        <table width="560"  border="0"><tr height="40"><td>
     	 <br /><c:if test="${empty event.modifyDate}"><a href="changePage.do" class="pstep">﹤回上一步</a></c:if> 
      	<c:if test="${!empty step2Upload}">
      	<c:if test="${uploadSuccess>0 and uploadErr<=0}">
      		<input type="submit" id="nextsubmit" value="上传完成！下一步﹥" name="submit1" class="nstep" ><br />
		</c:if></c:if>
		</td></tr></table>
     </s:form>
    <!-- </div>  -->